package cn.kgc.ssm.controller;

import cn.hutool.captcha.LineCaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 课工场
 * @date 2023/10/20
 * @description   获取验证码的控制层
 */
@Controller
@RequestMapping("captcha")
public class CaptchaController {


    @GetMapping("getCode")
    public void  getCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {

        // 如何生成图片验证码
        LineCaptcha lineCaptcha = new LineCaptcha(110, 50, 4, 3);
        // 图片中生成的验证码
        String code = lineCaptcha.getCode();

        // 将验证码存储在session域中  用于后续的登录验证
        HttpSession session = request.getSession();
        session.setAttribute("sessionCode",code);

        ServletOutputStream outputStream = response.getOutputStream();

        // 将验证码图片已流的形式 响应给前端浏览器  img
        lineCaptcha.write(outputStream);

        outputStream.close();

    }

}
